'''
使用了merge_grid_meo的合并卫星的气象数据
合并beijing_17_18_aq质量站数据

'''

import pandas as pd
from math import*
import matplotlib.pyplot as plt
from kdd2018.tools import *






            #print(df_aq_grid_drop.loc[index].values[0])
# 根据经纬度聚类质量观测站 和 气象观测站

# 读入数据文件
aq_data = pd.read_csv("assets/data/beijing_17_18_aq.csv")
meo_data = pd.read_csv("assets/output/merge_aq_meo/merge_grid_meo.csv")

# 开始合并流程
print("==================开始合并流程===============")
print("==================处理气象数据、增加对应的质量站列=========开始======")
df_meo = pd.DataFrame(meo_data,index=None,columns=['stationName','utc_time','temperature','pressure','humidity','wind_direction','wind_speed/kph'])
df_meo.columns = ['stationId_aq','utc_time','temperature','pressure','humidity','wind_direction','wind_speed/kph']


print("==================处理气象数据、增加对应的质量站列=========结束======")
#print(df_meo.tail(100))

print("==================处理质量数据集、删减到需要合并的数据集===开始============")
df_aq = pd.DataFrame(aq_data,index=None,columns=['stationId','utc_time','NO2','CO','SO2','PM2.5','PM10','O3'])
df_aq.columns = ['stationId_aq','utc_time','NO2','CO','SO2','PM2.5','PM10','O3']
df_aq = df_aq.drop_duplicates(['stationId_aq','utc_time'])

print("==================开始处理质量数据集、删减到需要合并的数据集===结束===============")
print("==================合并的数据集========开始=======")
merge_data = pd.merge(df_meo,df_aq,on=['stationId_aq','utc_time'],how='right')
df_merge = pd.DataFrame(merge_data)

print("==================合并的数据集========结束=======")
print('天气数据总记录数===》',df_aq.shape[0])
print('合并后总记录数===》',df_merge.shape[0])


# print("==================处理插值========开始=======")
# # 全字段插值方法
# def interpolation_all_columns():
#     pass
#
# print("==================处理插值========结束=======")
print("==================合并天气========开始=======")
meo_weather_data = pd.read_csv("assets/output/merge_aq_meo/merge_aq_meo.csv")
df_meo_weather = pd.DataFrame(meo_weather_data,index=None,columns=['stationId_aq','utc_time','weather'])
df_merge = pd.merge(df_merge,df_meo_weather,on=['stationId_aq','utc_time'],how='left')
print("==================合并天气========结束=======")
print("==================写入文件merge_aq_meo.csv========开始=======")
df_merge.to_csv('assets/output/merge_aq_meo/final_merge_aq_grid_meo2.csv',index=False)
print("==================写入文件merge_aq_meo.csv========结束=======")
